﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Make_Model
{
    public class Vara_Data_Make_Controller : Vara_Data_IMake_Actions
    {
        private static List<Vara_Data_IMake> _makes;
        public List<Vara_Data_IMake> Makes
        {
            get
            {
                if (_makes == null)
                    Refresh_Make_List();

                return _makes;
            }
        }

        public Vara_Data_IMake ReturnMake(int ID)
        {
            Vara_Data_IMake MakeToReturn;
            MakeToReturn = Makes.Find(delegate(Vara_Data_IMake Make) { return Make.ID == ID; });
            return MakeToReturn;
        }

        public void Refresh_Make_List()
        {
            DataTable DataTableMake;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableMake = NewSqlConnector.ExecuteProcedure("Return_Makes");
            }
            List<Vara_Data_IMake> ListOfMakes = new List<Vara_Data_IMake>();
            foreach (DataRow R in DataTableMake.Rows)
            {
                Vara_Data_IMake MakeToAppend = new Vara_Data_Make(int.Parse(R["ID"].ToString()));
                MakeToAppend.Name = R["Name"].ToString();
                MakeToAppend.Models = new Vara_Data_Model_Controller().ReturnModelsByMake(MakeToAppend);
                ListOfMakes.Add(MakeToAppend);
            }
            _makes = ListOfMakes;
        }
    }
}
